package com.machineapps.botesom.adapter;

import java.util.List;

import android.content.Context;
import android.view.*;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.machineapps.botesom.BoteSom;
import com.machineapps.botesom.R;
import com.machineapps.botesom.model.Category;

public class PageCategoryAdapter extends BaseAdapter {

	private Context context;

	private List<Category> categories;

	public PageCategoryAdapter(Context context, List<Category> categories) {
		this.context = context;
		this.categories = categories;
	}

	public int getCount() {
		return 4;
	}

	public Object getItem(int position) {
		Object category = null;

		if (this.categories != null && !this.categories.isEmpty() && this.categories.size() > position) {
			category = this.categories.get(position);
		}

		return category;
	}

	public long getItemId(int position) {
		long id = 0;

		if (this.categories != null && !this.categories.isEmpty() && this.categories.size() > position) {
			id = this.categories.get(position).getId();
		}

		return id;
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		Category category = (Category) getItem(position);

		LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		View view = inflater.inflate(R.layout.icon_category, null);

		if (category != null) {
			ImageView categoryImage = (ImageView) view.findViewById(R.id.category_id);
			categoryImage.setTag(category);
			String icon = category.getIcon().toUpperCase();
			if (icon.equalsIgnoreCase("category_favorite")) {
				categoryImage.setImageResource(R.drawable.category_favorite);
			} else if (icon.equalsIgnoreCase("category_nature")) {
				categoryImage.setImageResource(R.drawable.category_nature);
			}
			((BoteSom) context).registerForContextMenu(categoryImage);

			categoryImage.setOnClickListener(new ClickCategoryListener());

			TextView categoryText = (TextView) view.findViewById(R.id.category_name);
			categoryText.setText(category.getName().toUpperCase());
		}

		return view;
	}

	class ClickCategoryListener implements OnClickListener {

		@Override
		public void onClick(View view) {
			ImageView categoryImage = (ImageView) view;
			Category category = (Category) categoryImage.getTag();
			BoteSom boteSomActivity = (BoteSom) context;
			boteSomActivity.setCurrentCategory(category);
			boteSomActivity.loadButtons();
		}

	}

}